package huawei;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;

/**
 * @description:
 * @author: Zmx
 * @create: 2022-04-06 21:27
 **/
public class P1 {
    public static void main(String[] args) {

        HashMap<String, Integer> stringToIndex = new HashMap<>();
        HashMap<Integer, String> indexToString = new HashMap<>();
        int showCount[] = new int[50050];//最大词语的个数
        int res = 0;
        int topN, M;
        Scanner scanner = new Scanner(System.in);
        topN = scanner.nextInt();//出现频率最高的词语个数 topN
        M = scanner.nextInt();//文章数目 M
        scanner.nextLine();//去掉空格读取
        //标题串，正文串
        for (int i = 1; i <= 2 * M; i++) {
            String str = scanner.nextLine();
            String[] s = str.split(" ");
            for (String ss : s) {
                if (stringToIndex.get(ss) == null) {
                    stringToIndex.put(ss, ++res);//ss:单词 - res:下标
                    indexToString.put(res, ss);  //res:下标 - ss:单词
                }
                if (i % 2 == 1) {//标题串
                    showCount[stringToIndex.get(ss)] += 3;
                } else {//正文串
                    showCount[stringToIndex.get(ss)] += 1;
                }
            }
        }
        int countPos[][] = new int[res][2];//res: 词语个数 2: [0]词语对应的频率系数;[1]:字符串对应的编号
        for (int i = 0; i < res; i++) {
            countPos[i][0] = showCount[i];
            countPos[i][1] = i;
        }
        //按照频率系数进行降序排序
        Arrays.sort(countPos, (l, r) -> r[0] - l[0]);
        for (int i = 0, j = topN; j > 0; i++, j--) {
            System.out.print(indexToString.get(countPos[i]));
            if (j > 1) {
                System.out.println(" ");
            }
        }
    }
}

//记录标题中出现的频率、记录标题的顺序、记录正文词的顺序